Skip to content

Bound WAF request inspection and rate limiter memory - #49

Open
compnew2006 wants to merge 2 commits into
UserJoo9:mainfrom
compnew2006:codex/waf-stream-body-limit
Open

compnew2006 wants to merge 2 commits into
UserJoo9:mainfrom
compnew2006:codex/waf-stream-body-limit

Conversation

@compnew2006

@compnew2006 compnew2006 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Problem and impact

The WAF checked a declared request size, then buffered the full body for inspection. The WAF could exceed its configured per-request inspection budget when no length was declared, putting pressure on gateway memory and affecting other requests.

The per-source rate buckets and auto-ban histories also grew with traffic and
retained entries after their clients stopped making requests.

Solution

  • Count actual body bytes at the ASGI receive boundary before buffering. Stop inspecting at the first chunk that would exceed waf.max_body_bytes (1 MiB by default). Keep the early check for declared sizes.

  • Return HTTP 413 for body-size violations in strict mode. Requests at or below the limit are replayed intact to the route. Log mode records the overflow and forwards the full stream without applying firewall auto-bans. The existing local-subnet exemption and off mode bypass inspection.

  • Keep the existing request checks and response security headers. Document the difference between a WAF inspection budget and each route's own upload limit.

  • Bound endpoint buckets and auto-ban histories separately to 4,096 entries.
    Expire buckets on their route's configured window, retain only the timestamps
    needed to reach the auto-ban threshold, and sweep expired state every five
    seconds, including with no requests.

  • Preserve active counters on saturation. An untracked source on a rate-limited
    route gets 429 until a slot is available. A full auto-ban table stops tracking
    additional sources while the WAF continues to reject their individual violations.

Verification

  • GitHub Actions on Linux passed lint and all 1,080 tests on Python 3.12.
  • 66 security and Local Share tests passed locally. Tiny in-process fixtures confirm rejection before the remaining body is read, acceptance at the exact limit, full body replay, strict-only auto-banning, capacity handling, and expiry.
  • pyflakes api core quota tests run.py and git diff --check passed.
  • The local macOS full-suite run hit the existing updater auto-install fixture failure, which falls back to the tarball path and has no test tarball. This PR is based directly on main.

@compnew2006 compnew2006 changed the title Bound WAF request body inspection while streaming Bound WAF request inspection and rate limiter memory Nov 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant